package com.example.smartcity1.service.patient;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.HospitalListAdapter;
import com.example.smartcity1.bean.HospitalListBean;
import com.example.smartcity1.network.RetrofitClient;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HospitalListActivity extends AppCompatActivity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_list);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("医院推荐");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        initListViewData();
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void initView() {
        listView = findViewById(R.id.listView);
    }

    private void initListViewData() {
        RetrofitClient.serviceService().getHospitalListData().enqueue(new Callback<HospitalListBean>() {
            @Override
            public void onResponse(Call<HospitalListBean> call, Response<HospitalListBean> response) {
                HospitalListBean bean = response.body();
                if (bean != null && bean.getCode()==200) {
                    List<HospitalListBean.RowsDTO> rows = bean.getRows();
                    HospitalListAdapter adapter = new HospitalListAdapter(HospitalListActivity.this,R.layout.item_hospital_list,rows);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener((parent, view, position, id) -> {
                        Intent intent = new Intent(HospitalListActivity.this, HospitalPageActivity.class);
                        intent.putExtra("id",rows.get(position).getId());
                        startActivity(intent);
                    });
                }
            }
            @Override
            public void onFailure(Call<HospitalListBean> call, Throwable t) {
            }
        });
    }
}